use anyhow::Result;
use std::fs;
// use prost_build::Config;

fn main() -> Result<()> {
    fs::create_dir_all("src/pb")?;
    // let mut  config = Config::new();
    // config.out_dir("src/pb").type_attribute(".*", "#[derive(Serialize, Deserialize, Clone, Debug, Default)]");
    // config.compile_protos(&["../protos/messages.proto"],&["../protos"])?;
    // Ok(())
    tonic_prost_build::configure()
        .out_dir("src/pb")
        .type_attribute(
            "User",
            "#[derive(derive_builder::Builder)]\n#[builder(setter(into, strip_option), default)]",
        )
        .type_attribute(
            "QueryRequest",
            "#[derive(derive_builder::Builder)]\n#[builder(setter(into, strip_option), default)]",
        )
        .type_attribute(
            "RawQueryRequest",
            "#[derive(derive_builder::Builder)]\n#[builder(setter(into, strip_option), default)]",
        )
        .type_attribute(
            "TimeQuery",
            "#[derive(derive_builder::Builder)]\n#[builder(setter(into, strip_option), default)]",
        )
        .type_attribute(
            "IdQuery",
            "#[derive(derive_builder::Builder)]\n#[builder(setter(into, strip_option), default)]",
        )
        .type_attribute("User", "#[derive(sqlx::FromRow)]")
        .type_attribute("User", "#[derive(serde::Serialize, serde::Deserialize)]")
        // .type_attribute("User", r#"#[serde(rename_all = "camelCase")]\n"#)
        .message_attribute("User", "#[serde(rename_all = \"snake_case\")]")
        .field_attribute("User.email", r#"#[builder(setter(into))]"#)
        .field_attribute("User.name", r#"#[builder(setter(into))]"#)
        .field_attribute("RawQueryRequest.query", r#"#[builder(setter(into))]"#)
        .field_attribute(
            "TimeQuery.lower",
            r#"#[builder(setter(into, strip_option))]"#,
        )
        .field_attribute(
            "TimeQuery.upper",
            r#"#[builder(setter(into, strip_option))]"#,
        )
        .field_attribute(
            "QueryRequest.timestamps",
            r#"#[builder(setter(each(name="timestamp", into)))]"#,
        )
        .field_attribute(
            "QueryRequest.ids",
            r#"#[builder(setter(each(name="id", into)))]"#,
        )
        .type_attribute(
            ".*",
            "#[derive(Serialize, Deserialize, Clone, Debug, Default)]",
        )
        .compile_protos(
            &[
                "../protos/user-stats/messages.proto",
                "../protos/user-stats/rpc.proto",
            ],
            &["../protos"],
        )?;
    Ok(())
}
